package pl.edu.agh.ki.ewywiadowka.client.windows;

import java.util.LinkedList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gen2.table.client.FixedWidthFlexTable;
import com.google.gwt.gen2.table.client.FixedWidthGrid;
import com.google.gwt.gen2.table.client.ScrollTable;
import com.google.gwt.gen2.table.client.AbstractScrollTable.SortPolicy;
import com.google.gwt.gen2.table.override.client.HTMLTable.RowFormatter;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;
import com.google.gwt.user.client.ui.TextBox;

import pl.edu.agh.ki.ewywiadowka.client.Const;
import pl.edu.agh.ki.ewywiadowka.client.EWywiadowka;
import pl.edu.agh.ki.ewywiadowka.client.dto.StudentDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.TeacherDTO;
import pl.edu.agh.ki.ewywiadowka.client.windows.secretary.ModifyTeacherWindow;

public class SearchTeacherWindow extends GenericWindow implements ClickHandler,
		TableListener {

	private ScrollPanel _mainPanel = new ScrollPanel();
	private DockPanel _dockPanel = new DockPanel();

	private ScrollTable _scrollTable = null;
	private Button _okButton = new Button("Wybierz nauczyciela");

	private FixedWidthGrid _dataTable;
	private FixedWidthFlexTable _headerTable;

	private int _selectedRow = -1;

	private SearchTeacherWindow _me;

	private LinkedList<TeacherDTO> _teachers = null; // bo na poczatku nie ma

	private int _mode = -1;

	public SearchTeacherWindow(EWywiadowka wywiadowka, int mode) {
		super(wywiadowka);

		_me = this;

		_mode = mode;

		_okButton.addClickHandler(this);

		_dataTable = new FixedWidthGrid(0, 5);
		_dataTable.setBorderWidth(1);

		_headerTable = new FixedWidthFlexTable();
		_headerTable.setBorderWidth(1);
		fillHeader();

		_scrollTable = new ScrollTable(_dataTable, _headerTable);
		_scrollTable.fillWidth();
		_scrollTable.setHeight("400px");

		_dockPanel.add(_scrollTable, DockPanel.CENTER);
		_dockPanel.add(_okButton, DockPanel.SOUTH);

		_mainPanel.add(_dockPanel);
		this.initWidget(_mainPanel);

		Const.eWywiadowkaService
				.getTeachers(new AsyncCallback<LinkedList<TeacherDTO>>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert(caught.getMessage());
					}

					@Override
					public void onSuccess(LinkedList<TeacherDTO> result) {

						_teachers = result;

						_headerTable = new FixedWidthFlexTable();
						_headerTable.setBorderWidth(1);
						fillHeader();

						_dataTable = new FixedWidthGrid(_teachers.size(), 3);
						_dataTable.setBorderWidth(1);
						repaintTable();

						_dockPanel.remove(_scrollTable);

						_scrollTable = new ScrollTable(_dataTable, _headerTable);
						_scrollTable.setHeight("400px");
						_scrollTable.setVisible(true);

						_dataTable.addTableListener(_me);
						_scrollTable.setSortPolicy(SortPolicy.DISABLED);

						_dockPanel.add(_scrollTable, DockPanel.CENTER);
						_scrollTable.fillWidth();
					}

				});
	}

	private void repaintTable() {
		int i = 0;
		for (TeacherDTO teacher : _teachers)
			fillRow(i++, teacher);
	}

	private void fillRow(int row, TeacherDTO teacher) {

		RowFormatter rowFormatter = _dataTable.getRowFormatter();

		if (row % 2 == 0)
			rowFormatter.addStyleName(row, "tableRowStyle1");
		else
			rowFormatter.addStyleName(row, "tableRowStyle2");

		_dataTable.setHTML(row, 0, teacher.name.toString());
		_dataTable.setHTML(row, 1, teacher.surname.toString());
		_dataTable.setHTML(row, 2, teacher.telephone.toString());

	}

	private void fillHeader() {
		_headerTable.setHTML(0, 0, "Imię");
		_headerTable.setHTML(0, 1, "Nazwisko");
		_headerTable.setHTML(0, 2, "Telefon");
	}

	public void onClick(ClickEvent event) {
		if (event.getSource() == _okButton) {
			if (_selectedRow == -1) {
				Window.alert("Prosze wybrac ucznia");
			} else {
				final TeacherDTO teacher = _teachers.get(_selectedRow);
				Window.alert("Wybrano ucznia: \n" + teacher.toString());

				_mainPanel.remove(_dockPanel);

				if (_mode == 1) { // modyfikacja
					_mainPanel.add(new ModifyTeacherWindow(getWywiadowka(),
							teacher));
				} else if (_mode == 2) { //usuniecie
					Const.eWywiadowkaService.removeTeacher(teacher,
							new AsyncCallback<Const.Error.TeacherRemoveMsg>() {

								@Override
								public void onFailure(Throwable caught) {
									Window.alert(caught.getMessage());
								}

								@Override
								public void onSuccess(Const.Error.TeacherRemoveMsg result) {
									Window.alert(result.toString());

									_mainPanel.add(new SearchTeacherWindow(
											getWywiadowka(), 2));
								}

							});
				}

			}
		}
	}

	@Override
	public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
		RowFormatter rowFormatter = _dataTable.getRowFormatter();
		if (_selectedRow != -1) {
			if (_selectedRow % 2 == 0)
				rowFormatter.setStyleName(_selectedRow, "tableRowStyle1");
			else
				rowFormatter.setStyleName(_selectedRow, "tableRowStyle2");
		}
		_selectedRow = row;
		rowFormatter.setStyleName(row, "clickedTableRowStyle");
	}
}
